<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link type="text/css" rel="stylesheet" href="css/style.css" />
  <script src="./js/vue.js"></script>
  <script src="./js/md5.js"></script>
  <script src="./js/axios.js"></script>
  <style>
    #box{
      width:1000px;
      height:500px;
      border:1px solid black;
      margin:auto;
    }
    p{
      text-align:center;
    }

  </style>
</head>
<body>

  <div id="box">
    <p>新闻标题: <input type="text" v-model="newsTitle"></p>
    <p>新闻内容: <input type="text" v-model="newsContent"></p>
    <p>新闻时间: <input type="date" v-model="newsCreateTime"></p>
    <p>
      <button @click="updataData">修改</button>
    </p>
  </div>
  <script>
    new Vue({
      el:"#box",
      data:{
        newsId:0,
        newsTitle:"",
        newsContent:"",
        newsCreateTime:""
      },
      methods:{
        getParameter:function(par){
          //获取当前URL
          var local_url = document.location.href;
          //截取get字符串
          var getstr = local_url.substr(local_url.indexOf('?') + 1)
          //解析成get数组
          var post = getstr.split('&')
          //查找要找到参数（par）
          for ( var i in post) {
              if (post[i].indexOf(par + '=') >= 0) {
                  return post[i].replace(par + '=', '');
              }
          }
          //如果找不到返回false
          return false;
        },
        init:function(){
          axios({
            method:"GET",
            url:"http://localhost:8080/getInformation?id="+this.newsId,
          }).then(res => {
            this.newsTitle = res.data[0].title;
            this.newsContent = res.data[0].content;
            this.newsCreateTime = res.data[0].createTime;
          })
        },
        updataData:function(){
          if( this.newsTitle == "" ){
            alert("标题不能为空!");
            return;
          }
          if( this.newsContent == "" ){
            alert("内容不能为空!");
            return;
          }

          axios({
            method:"GET",
            url:"http://localhost:8080/updataInformationById?id="+this.newsId+"&title="+this.newsTitle+"&content="+this.newsContent+"&createTime="+this.newsCreateTime,
          }).then(res => {
            if( res.data == true ){
              alert("修改成功!");
              window.location.href = "./informationList.html";
            }
          })


        }
      },
      created:function(){
        this.newsId = this.getParameter("id");
        this.init(); 
      }
    })
  </script>

  
</body>
</html>